package me.sealer.ssm.service.impl;

import me.sealer.ssm.constant.ErrorCodeConstant;
import me.sealer.ssm.exception.SsmException;
import me.sealer.ssm.mapper.RoleMapper;
import me.sealer.ssm.mapper.UserMapper;
import me.sealer.ssm.mapper.UserRoleMapper;
import me.sealer.ssm.model.Role;
import me.sealer.ssm.model.RoleExample;
import me.sealer.ssm.model.User;
import me.sealer.ssm.model.UserExample;
import me.sealer.ssm.model.UserRole;
import me.sealer.ssm.model.UserRoleExample;
import me.sealer.ssm.service.UserRoleService;
import me.sealer.ssm.service.UserService;
import org.apache.shiro.util.Assert;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 功能描述： $ UserRoleImpl
 *
 * @author sealer
 * @email 1178884049@qq.com
 * @date 2019年11月26日 15时04分43秒
 */
@Service
public class UserRoleServiceImpl implements UserRoleService {
    private final UserRoleMapper userRoleMapper;

    private final RoleMapper roleMapper;
    private final UserMapper userMapper;
    private final UserService userService;

    public UserRoleServiceImpl(UserRoleMapper userRoleMapper, RoleMapper roleMapper, UserMapper userMapper, UserService userService) {
        this.userRoleMapper = userRoleMapper;
        this.roleMapper = roleMapper;
        this.userMapper = userMapper;
        this.userService = userService;
    }

    @Override
    public List<Role> getRolesByUsername(String username) {
        // 根据username查询User
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUsernameEqualTo(username);
        List<User> users = userMapper.selectByExample(userExample);

        // User不存在
        if (users.size() == 0) {
            throw new SsmException(ErrorCodeConstant.USER_USER_NOT_EXISTS, new Object[]{username});
        }

        // user有且仅有一个
        Assert.isTrue(users.size() == 1, "脏数据异常");

        User user = users.get(0);

        // 根据use获取userRoles
        UserRoleExample userRoleExample = new UserRoleExample();
        userRoleExample.createCriteria().andUserIdEqualTo(user.getId());
        List<UserRole> userRoles = userRoleMapper.selectByExample(userRoleExample);

        // 根据userRoles获取roles
        RoleExample roleExample = new RoleExample();
        roleExample.createCriteria().andIdIn(userRoles.stream().map(UserRole::getRoleId).collect(Collectors.toList()));
        return roleMapper.selectByExample(roleExample);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modifyUserRoles(String username, List<Integer> rolesIds) {
        User user = userService.queryUserByUsername(username);
        if (user == null) {
            throw new SsmException(ErrorCodeConstant.USER_USER_NOT_EXISTS, new Object[]{username});
        }

        int userId = user.getId();

        // 删除userRole
        UserRoleExample userRoleExample = new UserRoleExample();
        userRoleExample.createCriteria().andUserIdEqualTo(userId);
        userRoleMapper.deleteByExample(userRoleExample);

        rolesIds.forEach(id -> {
            UserRole userRole = new UserRole(userId, id);
            userRoleMapper.insert(userRole);
        });
    }
}
